<template>
    <div>

        <main>
            <div class="m-content">
                <el-form ref="editForm" status-icon :model="editForm" :rules="rules" label-width="80px">
                    <el-form-item label="标题" prop="title">
                        <el-input v-model="editForm.title"></el-input>
                    </el-form-item>
                    <el-form-item label="作者" prop="username" style="display: none">
                        <el-input v-model="editForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="作者id"  style="display: none">
                        <el-input v-model="editForm.userid"></el-input>
                    </el-form-item>
                    <el-form-item label="分类" prop="title">
                        <el-select v-model="editForm.type" placeholder="请选择分类">
                            <el-option v-for="dict in typeOptions"
                                       :key="dict.id"
                                       :label="dict.posttype"
                                       :value="dict.posttype"></el-option>

                        </el-select>
                    </el-form-item>
                    <el-form-item label="内容" prop="content">
                        <mavon-editor v-model="editForm.content"/>
                    </el-form-item>
                    <el-form-item>
                        帖子配图
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:8181/uploadimg"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('editForm')">立即发布</el-button>
                        <el-button>取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </main>
        <div class="post" >
            <el-image class="image"
                      style="width: 120px; height: 120px"
                      :src="user.avatar"
            >
            </el-image>
            <el-divider><i class="el-icon-edit"></i></el-divider>
            <h4>我的帖子发布数：{{user.postnumber}}</h4>
            <el-button class="bt" type="primary" plain >发布帖子</el-button>
            <br>
            <el-button class="bt" plain >我的帖子</el-button>
            <br>
            <el-button class="bt" plain>我的评论</el-button>
        </div>
    </div>
</template>

<script>
    export default {
        name: "addPost",
        data(){
            return{
                imageUrl:'',
                typeOptions:[],
                user:{
                    avatar:'',
                    uuid:'',
                    postnumber:'',
                },
                editForm: {
                    title: '',
                    username:'',
                    userid:'',
                    type:[],
                    description: '',
                    content: '',
                    image: '',
                },
                rules: {
                    title: [
                        {required: true, message: '请输入标题', trigger: 'blur'},
                        {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入摘要', trigger: 'blur'}
                    ],

                    type: [
                        { required: true, message: '请选择分类', trigger: 'change' },

                    ],
                }
            }
        },
        created() {
            const _this = this
            if(this.$store.getters.getUser.username) {
                this.user.username = this.$store.getters.getUser.username
                this.user.avatar = this.$store.getters.getUser.avatar
                this.user.uuid = this.$store.getters.getUser.id
                this.editForm.username = this.user.username
                this.editForm.userid = this.user.uuid
                this.user.postnumber = this.$store.getters.getUser.post
            }
            axios.get("http://localhost:8181/forum/listtype").then(function (resp) {
                _this.typeOptions = resp.data.data;
            });
        },
        methods:{
            submitForm(formName) {
                const _this = this
                console.log(this.editForm)
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("http://localhost:8181/forum/addPost",this.editForm).then(function (resp) {
                            if (resp.data.msg =='success'){
                                _this.$message({
                                    message: '发布成功！等待管理员审核',
                                    type: 'success'
                                });
                                window.location.reload()

                            }else {
                                _this.$message({
                                    message: '添加失败！',
                                    type: 'warning'
                                });
                            }

                        });

                    } else {

                        return false;
                    }
                });
            },
            handleAvatarSuccess(res, file) {
                const _this = this
                this.imageUrl = URL.createObjectURL(file.raw);
                _this.editForm.image = res.data
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>

<style scoped>
    main{
        width: 700px;
        height: 900px;
        margin-top: 20px;
        margin-left: 100px;
    }
    .post{
        justify-content: center;
        align-items: center;
        position: absolute;
        right: 80px;
        top: 135px;
        background-color: rgba(94, 255, 174,0.2);
        width: 300px;
        height: 500px;
        box-shadow: 0px 15px 10px -15px rgb(74, 131, 131);
        border-radius: 10px;
    }
    .image{
        margin-top: 20px;
    }
    .bt{
        margin: 10px !important;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 400px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 400px;
        height: 178px;
        display: block;
    }
</style>